var config = require('../../../utils/config.js')
var util = require('../../../utils/util.js')
var API_URL = config.API_URL
var app = getApp()
Page({
    data: {
        avatar: "",
    },
    onLoad: function (options) {
    },
    onReady: function () {
        // 生命周期函数--监听页面初次渲染完成
    },
    onShow: function () {
        // 生命周期函数--监听页面显示
        var data = app.globalData.userInfo;
        console.log(data)
        var that = this;
        util.PRL(API_URL.userInfo, {})
            .then(res => {
                var data = res.data.data;
                console.log(data)
                if (data.phone != "") {
                    data.phone_str = util.encrypt(data.phone);
                }
                if (data.email != "") {
                    data.email_str = util.encrypt(data.email);
                }
                that.setData(data);
                if (data.identity_status == 1) {
                    my.setStorage({
                        key: 'hasIdentified',
                        data: true,
                    })
                }
                if (data.live_type != '') {
                    my.setStorage({
                        key: 'hasBindLive',
                        data: true,
                    })
                }
            }).catch(res => {
                console.log(res);
            })
    },
    // 选择头像上传
    chooseImage: function (e) {
        //     console.log(e);
        var that = this;
        // my.chooseImage({
        //     count: 1,
        //     success: (res) => {
        //         var tempImage = res.apFilePaths[0];
        //         that.setData({
        //             avatar: tempImage
        //         });
        //         console.log(that.data.avatar)
        //         my.uploadFile({
        //             url: API_URL.upload_img,
        //             fileType: 'image',
        //             fileName: 'upload_file',
        //              filePath: tempImage,
        //             success: (res) => {
        //                 console.log(res)
        //                 let params = {
        //                     type: 'avatar',
        //                     value: res.data
        //                 }
        //                 return util.PRL(API_URL.edit_user, params)
        //             },
        //         });
        //     },
        // });
        let choose_P = util.wxPromise(my.chooseImage);
        let upload_P = util.wxPromise(my.uploadFile);
        choose_P({
            count: 1
        }).then(res => {
            var tempImage = res.apFilePaths[0];
            that.setData({
                avatar: tempImage
            });
            console.log(that.data.avatar)
            return upload_P({
                url: API_URL.upload_img,
                fileType: 'image',
                filePath: tempImage,
                fileName: 'upload_file',
            })
        }).then(res => {
            console.log(res)
            let params = {
                type: 'avatar',
                value: res.data
            }
            return util.PRL(API_URL.edit_user, params)
        }).then(res => {
            console.log(res)
            if (res.data.status == 200) {
                my.showToast({
                    content: '修改成功',
                    type: "success",
                    duration: 1000
                })
                setTimeout(function () {
                   my.redirectTo({
                        url: '/pages/user/info/info'
                    })
                }, 1000);

            }
        }).catch(res => {
            console.log(res);
        })
    },
    // 放大头像
    previewImage: function (e) {
        console.log(e);
        var that = this;
        var urls = new Array;
        var img = e.currentTarget.dataset.img;
        urls.push(img)
        // console.log(imgs)
        // switch (e.currentTarget.dataset.type) {
        //     case "logo": urls[0] = that.data.avatar; break;
        //     case "background": urls[0] = that.data.background; break;
        // }
        my.previewImage({
            current: 0,
            urls: urls
        })
    },
    // 修改名称
    edit: function (e) {
        console.log(e);
        var type = e.currentTarget.dataset.type;
        var value = e.currentTarget.dataset.value;
        // 获取到当前的名字发送给下一个路由请求页
        my.navigateTo({
            url: '/pages/user/edit_info/edit_info?type=user&name=' + type + '&value=' + value
        })
    },

    account: function (e) {
        my.navigateTo({
            url: '/pages/user/account/account'
        })
    },

    // 实名认证 
    certification: function (e) {
        var that = this;
        if (that.data.phone == "") {
            my.showModal({
                title: '提示',
                content: '您尚未绑定手机号，请先绑定。',
                confirmText: '去绑定',
                success: function (res) {
                    if (res.confirm) {
                        my.navigateTo({
                            url: '/pages/user/account/bind/bind?name=手机&typ=mobile',
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            my.navigateTo({
                url: '/pages/user/certification/certification',
            })
        }
    },
    // 绑定直播间
    live: function (e) {
        let phone = this.data.phone;
        let identity_status = this.data.identity_status;
        if (phone == "") {
            my.showToast({
                title: '提示',
                content: '您尚未绑定手机号，请先绑定。',
                confirmText: '去绑定',
                success: function (res) {
                    if (res.confirm) {
                        my.navigateTo({
                            url: '/pages/user/account/bind/bind?name=手机&typ=mobile',
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else if (identity_status != 1) {
            my.showToast({
                title: '提示',
                content: '您尚未实名认证，请先进行实名认证。',
                confirmText: '去实名',
                success: function (res) {
                    if (res.confirm) {
                        my.navigateTo({
                            url: '/pages/user/certification/certification',
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            my.navigateTo({
                url: '/pages/user/bind-live/bind-live?phone=' + phone,
            })
        }
    }
})